class Solution {
    public int strToInt(String str) {
        long num = 0;
        int len = str.length();
        int flag = 1;
        //long ret = 1;
        for(int i = 0;i<len;i++) {
            while(i < len && ' ' == str.charAt(i) ) {
                i++;
            }
            if(i < len && '-' == str.charAt(i)) {
                flag = -1;
                i++;
                System.out.println("-");
            }else if(i < len &&'+' == str.charAt(i)) {
                flag = 1;
                i++;
                System.out.println("+");
            }
            while(i < len && (str.charAt(i)<='9' && str.charAt(i) >= '0')) {
                num = num*10 + (str.charAt(i) - '0' )*flag;  
                if(num>Integer.MAX_VALUE) {
                    return Integer.MAX_VALUE;
                }
                if(num < Integer.MIN_VALUE){
                    return Integer.MIN_VALUE;
                } 
                i++; 
            }
            if(i < len && (str.charAt(i)>'9' || str.charAt(i) < '0')){
                return (int) num;

            }

        }
        return (int)num;
    }
}